package com.afd.ozblip.activity;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.util.Base64;
import android.view.View;
import android.widget.LinearLayout;

public class ChoosePhotoActivity extends BaseActivity {

	/****************** 视图变量 **************************/
	private LinearLayout settingLayout;

	/****************** 数据变量 **************************/

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Intent getImage = new Intent(Intent.ACTION_GET_CONTENT);
		getImage.addCategory(Intent.CATEGORY_OPENABLE);
		getImage.setType("image/*");
		startActivityForResult(getImage, 0);
	}

	/**
	 * 寻找视图
	 */
	public void findViews() {
	}

	/**
	 * 设置监听
	 */
	public void setListeners() {
		View.OnClickListener moreClick = new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.ll_more_layout_setting:
					Intent settingIntent = new Intent(ChoosePhotoActivity.this,
							SettingActivity.class);
					startActivity(settingIntent);
					break;

				default:
					break;
				}
			}
		};
	}

	/**
	 * 获取传递的参数
	 */
	private void getBundle() {
		Intent intent = getIntent();
	}

	private byte[] mContent;

	@Override
	protected void onActivityResult(int requestCode, int resultCode,
			Intent intent) {
		super.onActivityResult(requestCode, resultCode, intent);
		try {
			ContentResolver resolver = getContentResolver();
			// 获得图片的uri
			Uri originalUri = intent.getData();
			// 将图片内容解析成字节数组
			mContent = readStream(resolver.openInputStream(Uri
					.parse(originalUri.toString())));
			if (mContent == null) {
				return;
			}
			Intent indexIntent = new Intent(ChoosePhotoActivity.this,
					IndexActivity.class);
			indexIntent.putExtra("content", mContent);
			indexIntent.putExtra(IndexActivity.INTENT_TAB_CHANGE, 3);
			startActivity(indexIntent);
			finish();
		} catch (Exception e) {
			// System.out.println(e.getMessage());
		}
	}

	public static byte[] readStream(InputStream inStream) throws Exception {
		byte[] buffer = new byte[1024];
		int len = -1;
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		while ((len = inStream.read(buffer)) != -1) {
			outStream.write(buffer, 0, len);
		}
		byte[] data = outStream.toByteArray();
		outStream.close();
		inStream.close();
		return data;

	}
}